#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <list>
#include <jsoncpp/json/json.h>
#include "../../../NetEngine_Linux/NetEngine_Release/NetEngine_Include/XyRyNet_CommHdr.h"
#include "../../../NetEngine_Linux/NetEngine_Release/NetEngine_Include/XyRyNet_Types.h"
#include "../../../NetEngine_Linux/NetEngine_Release/NetEngine_Include/XyRyNet_ProtocolHdr.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_XMQCore/MQCore_XDDService/XDDS_Define.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_XMQCore/MQCore_XDDService/XDDS_Error.h"

//g++ -std=gnu++11 -Wall -g TestFor_DDSSub.cpp -o TestFor_DDSSub -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Core -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Client -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_NetHelp -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_XMQCore -lNetEngine_BaseLib -lNetEngine_Core -lNetClient_Socket -lNetHelp_APIHelp -lMQCore_XDDService -ljsoncpp -Wl,-rpath=../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Core:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Client:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_NetHelp:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_XMQCore,--disable-new-dtags
XNETHANDLE xhNetSub;
void __stdcall XDDS_CommApi_Callback(int nNotifyEvent, int nDomainId, NETENGINE_PROTOCOL_XDDS *pSt_XDDSProtocol, LPVOID lParam)
{
    printf("域ID:%d,主题:%s创建消息\n", nDomainId, pSt_XDDSProtocol->tszTopic);
    XDDS_SubScribe_Insert(&xhNetSub, pSt_XDDSProtocol);
}
int main(int argc, char **argv)
{
    if (!XDDS_CommApi_Init("192.168.1.109"))
    {
        printf("初始化失败!\n");
        return -1;
    }
    XDDS_CommApi_SetAttr(XDDS_CommApi_Callback);
    if (!XDDS_CommApi_DomainCreate(1000))
    {
        printf("XDDS_CommApi_DomainCreate 失败!\n");
        return -1;
    }
    if (!XDDS_CommApi_TopicCreate(1000, "aa"))
    {
        printf("XDDS_CommApi_TopicCreate 失败!\n");
        return -1;
    }

    XNETHANDLE xhSubcribe;
    if (!XDDS_SubScribe_Create(&xhSubcribe, 1000, "aa"))
    {
        printf("XDDS_Publish_Create 失败!\n");
        return -1;
    }
    printf("ok!\n");
    int nTime = 0;
    while (nTime < 99999)
    {
        int nLen = 2048;
        TCHAR tszMsgBuffer[2048];
        memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));

        if (XDDS_SubScribe_ReadMsg(xhSubcribe, tszMsgBuffer,&nLen))
        {
            nTime++;
            printf("recv:%d-%s\n",nLen,tszMsgBuffer);
        }
        if (XDDS_SubScribe_ReadMsg(xhNetSub, tszMsgBuffer, &nLen))
        {
            nTime++;
            printf("recv notify:%d-%s\n", nLen, tszMsgBuffer);
        }
        Sleep(1000);
    }

    XDDS_SubScribe_Delete(xhSubcribe);
    XDDS_CommApi_TopicDelete(1000, "aa");
    XDDS_CommApi_DomainDelete(1000);
    XDDS_CommApi_Destory();
    return 0;
}
